package com.sdry.web.controller.approvalManagement;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.sdry.entity.approvalManagement.ModelEntity;
import com.sdry.service.ApprovalInitiateService;
import com.sdry.service.approvalManagement.FormManagementService;

/**
 * 
 * @ClassName: FormManagementController 
 * @Description:表单管理
 * @Author zc
 * @Date 2018年8月20日 上午10:30:00 
 * @version 1.0
 */
@Controller
@RequestMapping("formManagement")
public class FormManagementController {

	@Resource
	FormManagementService fmService;
	@Resource
	ApprovalInitiateService approvalInitiateService;
	/**
	 * 进入新建表单页面
	 * @return
	 */
	@RequestMapping("newForm")
	public String enterNewForm(){
		return "/approvalManagement/newForm";
	}

	/**
	 * 保存模板
	 * @param request
	 * @param response
	 * @param formName
	 * @param array
	 */
	@RequestMapping("/saveModel")
	public void saveModel(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "formName") String formName, 
			@RequestParam(value = "array[]") List<String> array,
			@RequestParam(value = "arrayType[]") List<String> arrayType){
		//表单字段处理
		String str = array.toString().trim();
		String strType = arrayType.toString().trim();
		
		String mFields = str.substring(1, str.length()-1);
		String mFieldsType = strType.substring(1, strType.length()-1);
		
		ModelEntity modelEntity = new ModelEntity();
		//表单名称
		modelEntity.setmName(formName);
		//字段名称
		modelEntity.setmFields(mFields);
		//字段类型
		modelEntity.setRemark01(mFieldsType);
		int isSave = fmService.saveModel(modelEntity);
		try {
			response.getWriter().print(isSave);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据ID查询审批模板
	 * @param mId
	 * @return
	 */
	@RequestMapping(value="/findFormByID")
	public ModelAndView findModelByID(long mId){
		ModelAndView mv = new ModelAndView();
		ModelEntity modelEntity = approvalInitiateService.findModelByID(mId);
		Map<String, String> map = new LinkedHashMap<String, String>();
		//List<String> mFieldList = new ArrayList<>();
		//List<String> mFieldTypeList = new ArrayList<>();
		String[] mFieldArr = modelEntity.getmFields().split(", ");
		String[] mFieldTypeArr = modelEntity.getRemark01().split(", ");
		for (int i = 0; i < mFieldArr.length; i++) {
			map.put(mFieldArr[i], mFieldTypeArr[i]);
			//mFieldList.add(mFieldArr[i]);
			//mFieldTypeList.add(mFieldTypeArr[i]);
		}
		//modelEntity.setmFieldList(mFieldList);
		//modelEntity.setmFieldTypeList(mFieldTypeList);
		mv.addObject("model", map);
		mv.setViewName("/approvalManagement/formDetail");
		return mv;
	}
}
